<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" :hide-required-asterisk="true">
    <el-form-item label="工号" prop="id">
      <el-input v-model.number="ruleForm.id"></el-input>
    </el-form-item>
    <el-form-item label="登录密码" prop="pwd">
      <el-input type="password" v-model="ruleForm.pwd"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="角色" prop="role">
      <el-select v-model="ruleForm.role" placeholder="请选择您的身份">
        <el-option label="指导老师" value="1"/>
        <el-option label="专业负责人" value="2"/>
        <el-option label="系主任" value="3"/>
        <el-option label="教务处" value="4"/>
      </el-select>
    </el-form-item>
    <el-form-item label="系别" prop="major">
      <el-select v-model="ruleForm.major" placeholder="请选择您的系别">
        <el-option label="软件工程系" value="5"/>
        <el-option label="网络技术系" value="6"/>
        <el-option label="游戏系" value="7"/>
        <el-option label="计算机系" value="8"/>
        <el-option label="数码媒体系" value="9"/>
        <el-option label="电子系" value="10"/>
        <el-option label="外语系" value="11"/>
        <el-option label="管理系" value="12"/>
        <el-option label="财经系" value="13"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      ruleForm: {
        id: '',
        name: '',
        role: '',
        pwd: '',
        major:'',
      },
      rules: {
        id: [
          {required: true,message: '请输入工号', trigger: 'blur'},
          {min:1000,max:9999,type: 'number', message: '请输入4位数字', trigger: 'blur'}
        ],
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        role:[{required: true, message: "请选择角色", trigger: 'change'}],
        major:[{required: true, message: "请选择系别", trigger: 'change'}]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          axios.post('/teachers/add', {
            id: this.ruleForm.id,
            name: this.ruleForm.name,
            role: this.ruleForm.role,
            pwd: this.ruleForm.pwd,
            major:this.ruleForm.major,
          }).then(res => {
            let r = res.data
            if (r.flag === true) {
              alert(r.msg)
              this.$router.push('/all')
            } else {
              alert(r.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        } else {
          console.log('请检查是否有遗漏或者不符合要求的');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  }
}
</script>